package com.yuebao.mq;

import com.yuebao.exception.CustomerException;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.MessageModel;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Optional;


@RocketMQMessageListener(topic = "fund-service",consumerGroup = "${rocketmq.consumer.group}",messageModel = MessageModel.BROADCASTING )
@Component
@Slf4j
public class FundEventReceiver implements RocketMQListener<MessageExt> {
    
    @Resource
    private EventMqReceiver eventMqReceiver;
    
    @Override
    public void onMessage(MessageExt message) {
        byte[] body = new byte[0];
        try {
            body = Optional.ofNullable(message.getBody())
                    .orElseThrow(() -> new CustomerException(String.format("event消息为空, messageId:【%s】", message.getMsgId())));
        } catch (CustomerException e) {
            e.printStackTrace();
        }
        String eventString = new String(body);
        String messageId = message.getMsgId();
        log.info("message start consume==》messageId:【{}】, event: 【{}】", message.getMsgId(), eventString);
        try {
            eventMqReceiver.handleReceiveMessage(eventString, messageId);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    //todo 更改订单支付状态
    
    
    //成功
    
    //失败
}
